//
//  UIViewController+YHRouter.m
//  YHRouter
//
//  Created by 98du on 2021/12/1.
//

#import "UIViewController+YHRouter.h"
#import <objc/runtime.h>
#import "YHRouter.h"



@implementation UIViewController(YHRouterPrivite)


-(YHRouterParam *)yh_routerParams{
    return objc_getAssociatedObject(self, @selector(yh_routerParams));
}

-(void)setYh_routerParams:(YHRouterParam *)yh_routerParams{
    objc_setAssociatedObject(self, @selector(yh_routerParams), yh_routerParams, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIViewController *(^)(YHRouterParam * params))yh_reloadParams{
    UIViewController *(^block)(YHRouterParam * params) = ^UIViewController *(YHRouterParam * params) {
        if(!params){
            return self;
        }
        if([self respondsToSelector:@selector(yh_routerPassReloadParams:)]){
            [self yh_routerPassReloadParams:params];
        }
        return self;
    };
    return block;
}

/// 该控制器只能有一个， 如果已有则刷新该控制器数据
- (UIViewController *(^)(void))yh_singleton{
    UIViewController *(^block)(void) = ^UIViewController *(void) {
        if(![self respondsToSelector:@selector(yh_routerIsSingleton)]){
            return self;
        }
        BOOL isSingleton = [self yh_routerIsSingleton];
        if(!isSingleton){
            return self;
        }
        YHRouterParam * params = self.yh_routerParams;
        
        __block UIViewController * exitVC = nil;
        [YHCurrentViewController().navigationController.viewControllers enumerateObjectsUsingBlock:^(UIViewController *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if(obj.class == self.class){
                exitVC = obj;
                *stop = YES;
            }
        }];
        if(exitVC){
            [YHCurrentViewController().navigationController popToViewController:exitVC animated:YES];
            exitVC.yh_reloadParams(params);
            if([YHRouter sharedManager].errorHandler){
                [YHRouter sharedManager].errorHandler(YHRouterErrorCodeControllerSingleton, [NSString stringWithFormat:@"已有该控制器:%@，不做跳转",self.class]);
            }
            return nil;
        }
        return self;
    };
    return block;
}

/// 该控制器需要登录操作
- (UIViewController *(^)(void))yh_needLogin{
    UIViewController *(^block)(void) = ^UIViewController *(void) {
        if([self respondsToSelector:@selector(yh_routerNeedLoginFirst)]){
            BOOL needLogin = [self yh_routerNeedLoginFirst];
            if(needLogin){
                if([YHRouter sharedManager].errorHandler){
                    [YHRouter sharedManager].errorHandler(YHRouterErrorCodeNeedLogin, [NSString stringWithFormat:@"控制器:%@，需要先做登录操作",self.class]);
                }
                return nil;
            }
        }
        return self;
    };
    return block;
}


@end



@implementation UIViewController (YHRouter)


-(YHRouterCallBlock)yh_callback{
    return objc_getAssociatedObject(self, @selector(yh_callback));
}

-(void)setYh_callback:(YHRouterCallBlock)yh_callback{
    objc_setAssociatedObject(self, @selector(yh_callback), yh_callback, OBJC_ASSOCIATION_COPY_NONATOMIC);
}


- (UIViewController *(^)(YHRouterParam * params))yh_params{
    UIViewController *(^block)(YHRouterParam * params) = ^UIViewController *(YHRouterParam * params) {
        if(!params){
            return self;
        }
        if([self respondsToSelector:@selector(yh_routerPassParams:)]){
            [self setYh_routerParams:params];
            [self yh_routerPassParams:params];
        }
        return self;
    };
    return block;
}





- (UIViewController *(^)(void))yh_push{
    UIViewController *(^block)(void) = ^UIViewController *(void) {
        [YHRouter yh_pushViewController:self];
        return self;
    };
    return block;
}

- (UIViewController *(^)(void))yh_pop{
    UIViewController *(^block)(void) = ^UIViewController *(void) {
        [self.navigationController popViewControllerAnimated:YES];
        return self;
    };
    return block;
}

- (UIViewController *(^)(void))yh_present{
    UIViewController *(^block)(void) = ^UIViewController *(void) {
        [YHRouter yh_presentViewController:self];
        return self;
    };
    return block;
}

- (UIViewController *(^)(void))yh_dismiss{
    UIViewController *(^block)(void) = ^UIViewController *(void) {
        [self dismissViewControllerAnimated:YES completion:nil];
        return self;
    };
    return block;
}


@end

